package com.Iconcat.system.util;

import lombok.extern.flogger.Flogger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.logging.Logger;

public class ImageCodeUtil {
    private static final String CODES = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZA"; // 0123456789壹贰叁肆伍陆柒捌玖拾
    private static final Font FONT = new Font(null,   Font.HANGING_BASELINE, 40);
    private static final Random RANDOM = new Random();
    private static final int LENGTH = 4;
    private static final int MAX_LENGTH = 10;
    private static final int AMOUNT = 50;

    private static final FontMetrics FONT_METRICS;
    static {
        BufferedImage image = new BufferedImage(120, 1, BufferedImage.TYPE_INT_ARGB);
        Graphics graphics = image.getGraphics();
        graphics.setFont(FONT);
        FONT_METRICS = graphics.getFontMetrics();
    }

    public static String makeCode() {
        return makeCode(LENGTH);
    }

    public static String makeCode(int length) {
        length = length < LENGTH ? LENGTH : length;
        length = length > MAX_LENGTH ? MAX_LENGTH : length;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(CODES.charAt(RANDOM.nextInt(CODES.length())));
        }
        return sb.toString();
    }

    public static BufferedImage makeImage(String code) {
        int height = FONT_METRICS.getHeight(); // 绘图高度
        int width = FONT_METRICS.stringWidth(code); // 绘图宽度
        int ascent = FONT_METRICS.getMaxAscent();

        // 创建内存图片
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = image.createGraphics();
        graphics.setFont(FONT);

        // 绘制渐变背景
        Color startColor = getRandomColor(200, 250);
        Color endColor = getRandomColor(200, 250);
        GradientPaint gradient = new GradientPaint(0, 0, startColor, width, height, endColor);
        graphics.setPaint(gradient);
        graphics.fillRect(0, 0, width, height);

        // 绘制文字
        for (int i = 0; i < code.length(); i++) {
            char c = code.charAt(i);
            graphics.setColor(getRandomColor(20, 160)); // 随机文字颜色
            graphics.drawString(String.valueOf(c), i * width / code.length(), height / 2 + ascent / 2 - 2);
        }

        // 添加噪点
        for (int i = 0; i < AMOUNT; i++) {
            int x = RANDOM.nextInt(width);
            int y = RANDOM.nextInt(height);
            graphics.setColor(getRandomColor(160, 200)); // 随机噪点颜色
            graphics.fillRect(x, y, 2, 2);
        }

        return image;
    }

    private static Color getRandomColor(int min, int max) {
        int r = min + RANDOM.nextInt(max - min);
        int g = min + RANDOM.nextInt(max - min);
        int b = min + RANDOM.nextInt(max - min);
        return new Color(r, g, b);
    }

    public static void main(String[] args) {
//        System.out.println(makeCode());
//        System.out.println(makeCode(20));
//        System.out.println(makeCode(3));
//
//        try {
//            ImageIO.write(makeImage(makeCode()), "jpeg", new FileOutputStream("test.jpg"));
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        System.out.println("finish...");

    }
}
